﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Commander_Wars.Spieletools
Imports System.Net.Sockets
''' <summary>
''' Informationen ueber die Netzwerkteilnehmer
''' </summary>
''' <remarks></remarks>
Public Class MemberInfo
    Public ID As Integer 'the ID assigned to this used (by the server)
    Public Flags As MemberFlag = MemberFlag.Lobby
    Public Username As String = ""
    Public Sub New(ByVal ID As Integer)
        Me.ID = ID
        Flags = MemberFlag.Lobby
    End Sub
    Public Sub New(ByVal ID As Integer, ByVal Bereit As MemberFlag)
        Me.ID = ID
        Me.Flags = Flags
    End Sub
    Public Sub New(ByVal ID As Integer, ByVal Name As String)
        Me.ID = ID
        Me.Username = Name
        Flags = MemberFlag.Lobby
    End Sub
    Public Sub New(ByVal ID As Integer, ByVal Name As String, ByVal Bereit As MemberFlag)
        Me.ID = ID
        Me.Username = Name
        Me.Flags = Bereit
    End Sub
    Public Sub New()
        Flags = MemberFlag.Lobby
        Username = DataSettings.Autor
        ID = Hauptmenue.Lobby.FindNewMemberID
    End Sub
    Public Sub New(ByVal Data() As String)
        Flags = Data(1)
        Username = Data(2)
        ID = Data(0)
    End Sub
    Enum MemberFlag
        Lobby = 1
        Joined = 2
        InGame = 4
        Host = 8
    End Enum
    Public Overrides Function ToString() As String
        Return ID.ToString + "_" + CInt(Flags).ToString + "_" + Username.ToString
    End Function
End Class
''' <summary>
''' Spielinformationen
''' </summary>
''' <remarks></remarks>
Public Class GameInfo
    Public CreatorID, MaxPlayers As Integer
    Public GameID As UInteger
    Public Name As String
    Public Version As Version
    Public Flags As GameFlags
    Public PlayerIDs As List(Of Integer)
    Public Password As String
    Public HostAdress As System.Net.IPAddress
    Public CurrentPlayers As Integer
    Public WrongGame As Boolean
    Enum GameFlags
        Open = 0 'An Open Game for everyone without a Password
        Locked = 1 'Requires a password to enter
        Closed = 2 ' No-one can enter
        InProgress = 4 'The game is currently being played
    End Enum
    Public Sub New(ByVal GameID As UInteger, ByVal CreatorID As Integer, ByVal MaxPlayers As Integer, ByVal Name As String, ByVal Version As Version, ByVal Password As String, ByVal HostAdress As System.Net.IPAddress)
        Me.CreatorID = CreatorID
        Me.MaxPlayers = MaxPlayers
        Me.Name = Name
        Me.Version = Version
        Me.Password = Password
        If Password = "" Then
            Flags = GameFlags.Open
        Else
            Flags = GameFlags.Locked
        End If
        PlayerIDs = New List(Of Integer)
        Me.HostAdress = HostAdress
        Me.GameID = GameID
    End Sub
    Public Sub New(ByVal GameData As String())
        CreatorID = GameData(1)
        MaxPlayers = GameData(2)
        GameID = GameData(3)
        Name = GameData(4)
        Version = New Version(GameData(5))
        Flags = [Enum].Parse(GameInfo.GameFlags.Closed.GetType, GameData(6))
        PlayerIDs = New List(Of Integer)
        Dim I As Integer() = ToIntArry(GameData(7).Split("_"))
        For I2 = 0 To I.Length - 1
            PlayerIDs.Add(I(I2))
        Next
        CurrentPlayers = CInt(GameData(8))
        Password = GameData(9)

        Dim IPName As String = GameData(10) + ":"
        For I2 = 11 To GameData.Length - 1
            If I2 = GameData.Length - 1 Then
                IPName += GameData(I2)
            Else
                IPName += GameData(I2) + ":"
            End If
        Next
        If Not System.Net.IPAddress.TryParse(IPName, HostAdress) Then
            WrongGame = True
        Else
            WrongGame = False
        End If

    End Sub
    Public Overrides Function tostring() As String
        If HostAdress Is Nothing Then
            Return "Game:" + CreatorID.ToString + ":" + MaxPlayers.ToString + ":" + GameID.ToString + ":" + Name + ":" + Version.ToString + ":" + _
            Flags.ToString + ":" + IntArrayToStr(PlayerIDs.ToArray) + ":" + CurrentPlayers.ToString + ":" + Password + ":" + GetIP.ToString
        Else
            Return "Game:" + CreatorID.ToString + ":" + MaxPlayers.ToString + ":" + GameID.ToString + ":" + Name + ":" + Version.ToString + ":" + _
            Flags.ToString + ":" + IntArrayToStr(PlayerIDs.ToArray) + ":" + CurrentPlayers.ToString + ":" + Password + ":" + HostAdress.ToString
        End If
    End Function
    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If MyBase.Equals(obj) Then
            Return True
        Else
            If TypeOf obj Is GameInfo Then
                Dim G As GameInfo = obj
                If G.Name = Name Then
                    If System.Net.IPAddress.Equals(HostAdress, G.HostAdress) Then
                        Return True
                    End If
                End If
            End If
        End If
    End Function
End Class
''' <summary>
''' Die Lobby
''' </summary>
''' <remarks></remarks>
Public Class BaseLobby
    Const YStart As Integer = 200
    Const XStart As Integer = 20
    Const PlatzUnten As Integer = 120
    Public Const LobbyUpdateTime As Integer = 60
    Const RightBorderWidth As Integer = 100
    Public ClientID As Integer = -1
    Public CurrentGame As GameInfo
    Public Games As New List(Of GameInfo)
    Dim Scrollbar As ObjektYScrollbar
    'Public Connection As LobbyConnection
    Dim Text As ObjektText
    ''' <summary>
    ''' Die User dieser Lobby
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Members As List(Of MemberInfo)
    Public Breiten(4) As Integer
    Dim FirstGame As Integer = 0

    Dim UpdateLobby As Integer = 0
    Public Updating As Boolean = False
    Public Connection As LobbyConnection
    ''' <summary>
    ''' Loads the Lobby into the RAM
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        'Load initial Lobby stuff
        Members = New List(Of MemberInfo)
        Games = New List(Of GameInfo)

        If DataSettings.LobbyHost Then
            Connection = New LobbyConnection
            Connection.IniConnection(True)
            Members.Clear()
            'Create Lobby Hostmember
            Members.Add(New MemberInfo(Integer.MinValue, DataSettings.Autor, MemberInfo.MemberFlag.Host))
            ClientID = Integer.MinValue 'Host is always the First ID avaiable.
        Else
            Connection = New LobbyConnection
            Dim IP As System.Net.IPAddress = GetIP(DataSettings.LobbyServerIP)
            If IP IsNot Nothing Then
                Connection.IniConnection(False, IP)
            End If
        End If


    End Sub

    Public Sub IniLobby()
        CurrentGame = Nothing
        Text = New ObjektText(New String() {}, 0, 0, Nothing, Schriftengine.Schriftarten.Courier_New, 16, FontStyle.Regular, False, 3)
        CreateScrollbar()
        ChangeWindowSize(New Point)
    End Sub
    Public Sub ChangeWindowSize(ByVal PosChange As System.Drawing.Point)
        Dim ToBig As Integer = 0
        Breiten(0) = Math.DivRem(DataSettings.BildschirmBreite - XStart * 2 - RightBorderWidth, 5, 0)
        Breiten(1) = Breiten(0)
        If Breiten(0) > 85 Then
            Breiten(2) = 85
            ToBig += (Breiten(0) - 85)
        Else
            Breiten(2) = Breiten(0)
        End If
        If Breiten(0) > 120 Then
            Breiten(3) = 120
            ToBig += (Breiten(0) - 120)
        Else
            Breiten(3) = Breiten(0)
        End If
        If Breiten(0) > 150 Then
            Breiten(4) = 150
            ToBig += (Breiten(0) - 150)
        Else
            Breiten(4) = Breiten(0)
        End If
        Breiten(0) += ToBig / 2
        Breiten(1) += ToBig / 2
        CreateScrollbar()

    End Sub
    Private Sub CreateScrollbar()
        If Scrollbar IsNot Nothing Then
            Objekt2D.RemoveObjekt(Scrollbar)
        End If
        If Grafikengine.Sprite IsNot Nothing Then
            Dim GesamtBreite As Integer = 0
            For I = 0 To Breiten.Length - 1
                GesamtBreite += Breiten(I)
            Next
            Dim Hoehe As Integer = DataSettings.BildschirmHoehe - YStart - PlatzUnten
            Dim BuchstabenDimension As Size = Text.GetSchriftart.MeasureString(Grafikengine.Sprite, "A", DrawTextFormat.None, Color.Black).Size
            Scrollbar = New ObjektYScrollbar(0, (Math.DivRem(Hoehe, BuchstabenDimension.Height + 4, 0) - 1) * (BuchstabenDimension.Height + 4) + 4, New Point(XStart + GesamtBreite + 1, YStart + BuchstabenDimension.Height + 4), New PointF(0, 0))
        End If
    End Sub
    ''' <summary>
    ''' Closes the Lobby.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CloseLobby()
        Games.Clear()
        Connection.Close()
        Hauptmenue.Lobby = Nothing
    End Sub

    ''' <summary>
    ''' Wir haben ein paar Daten empfangen!
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub NetzBefehlRecieved(ByVal Paket As NetworkPaket)
        Dim sinfo() As String = Paket.ReadString.Split(":")
        Select Case sinfo(0).ToLower
            Case "removegame" 'Removes the Game with this ID from the Game
                RemoveGame(CUInt(sinfo(1)))
            Case "playercreated" 'Create new Member on all Computers
                If DataSettings.LobbyHost Then
                    Dim Data() As String = sinfo(1).Split("_")
                    For I = 0 To Members.Count - 1
                        If Members(I).ID = CInt(Data(0)) Then
                            Members(I).Username = Data(2)
                            Exit For
                        End If
                    Next
                Else
                    Dim ID As Integer = sinfo(1).Split("_")(0)
                    For I = 0 To Members.Count - 1
                        If Members(I).ID = ID Then Exit Sub 'This ID exists already Exit
                    Next
                    Members.Add(New MemberInfo(sinfo(1).Split("_")))
                End If
            Case "playerdata" 'Host gaves us our Player Info File.
                Members.Add(New MemberInfo(sinfo(1).Split("_")))
                Members(Members.Count - 1).Username = DataSettings.Autor
                ClientID = Members(Members.Count - 1).ID 'Save our own ID.
                Connection.SendData("Playercreated:" + Members(Members.Count - 1).ToString) 'Send our Data back to the Host.
            Case "killplayer" 'Kills this Player
                KillPlayer(CInt(sinfo(1)))
            Case "game"
                Games.Add(New GameInfo(sinfo))
            Case "updategamestart"
                While Updating
                    Threading.Thread.Sleep(ThreadSleepTime)
                End While
                Updating = True
                If Not DataSettings.LobbyHost Then
                    Games.Clear()
                End If
            Case "updategameend"
                Updating = False
            Case "gamecreated"
                Dim IP As System.Net.IPAddress = Nothing
                Dim IPName As String = sinfo(6) + ":"
                For I = 7 To sinfo.Length - 1
                    If I = sinfo.Length - 1 Then
                        IPName += sinfo(I)
                    Else
                        IPName += sinfo(I) + ":"
                    End If
                Next
                If System.Net.IPAddress.TryParse(IPName, IP) Then
                    Games.Add(New GameInfo(FindGameID, CInt(sinfo(1)), sinfo(2), sinfo(3), New Version(sinfo(4)), sinfo(5), IP))
                End If
                If Connection.GetServer IsNot Nothing Then
                    Connection.GetServer.AddPaket(New ServerPaket("GameID:" + Games(Games.Count - 1).GameID.ToString, sinfo(1), False, True))
                End If
            Case "gameid"
                CurrentGame.GameID = CUInt(sinfo(1))
            Case "joinedgame"
                JoinedGame(CUInt(sinfo(1)), CInt(sinfo(2)))
            Case "exitgame"
                ExitGame(CUInt(sinfo(1)), CInt(sinfo(2)))
            Case "kiplayers"
                KIPlayers(CUInt(sinfo(1)), CInt(sinfo(2)))
            Case Else
                sinfo = Paket.ReadString.Split("|")
                Select Case sinfo(0).ToLower
                    Case "chat"
                        Hauptmenue.Chatroom.getMessage(New Chat.Message(sinfo(3), sinfo(2), -1))
                End Select
        End Select
    End Sub
    Public Sub JoinedGame(ByVal GameID As UInteger, ByVal PlayerID As Integer)
        If DataSettings.LobbyHost Then
            For I = 0 To Games.Count - 1
                If Games(I).GameID = GameID Then
                    Games(I).PlayerIDs.Add(PlayerID)
                    Exit For
                End If
            Next
        End If
    End Sub
    Public Sub ExitGame(ByVal GameID As UInteger, ByVal PlayerID As Integer)
        If DataSettings.LobbyHost Then
            For I = 0 To Games.Count - 1
                If Games(I).GameID = GameID Then
                    For I2 = 0 To Games(I).PlayerIDs.Count - 1
                        If Games(I).PlayerIDs(I2) = PlayerID Then
                            Games(I).PlayerIDs.RemoveAt(I2)
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next
        End If
    End Sub
    Public Sub KIPlayers(ByVal GameID As UInteger, ByVal Players As Integer)
        If DataSettings.LobbyHost Then
            For I = 0 To Games.Count - 1
                If Games(I).GameID = GameID Then
                    Games(I).CurrentPlayers = Players
                    Exit For
                End If
            Next
        End If
    End Sub
    ''' <summary>
    ''' Wir wollen ein paar Daten senden!
    ''' </summary>
    ''' <param name="Daten"></param>
    ''' <remarks></remarks>
    Public Sub NetzBefehlSenden(ByVal Daten As String)
        Connection.SendData(Daten)
    End Sub
    ''' <summary>
    ''' Malt die Ganze Lobby
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Malen()
        If Scrollbar Is Nothing Then
            CreateScrollbar()
        End If

        Updating = True
        'Testcode :)
        'If Games.Count = 0 Then
        '    For I = 0 To 30
        '        Games.Add(New GameInfo(I, ClientID, I, I.ToString, New Version(1, 0, 0, 0), "", Nothing))
        '    Next
        'End If
        Dim sngrotation As Single = 0
        Dim intColor As Color = Color.FromArgb(255, 255, 255, 255)
        Grafikengine.Sprite.Transform = Matrix.Identity
        Dim BuchstabenDimension As Size = Text.GetSchriftart.MeasureString(Grafikengine.Sprite, "A", DrawTextFormat.None, Color.Black).Size

        Dim Hoehe As Integer = DataSettings.BildschirmHoehe - YStart - PlatzUnten

        Grafikengine.Sprite.End()
        Dim GesamtBreite As Integer = 0
        For I = 0 To Breiten.Length - 1
            GesamtBreite += Breiten(I)
        Next
        Dim Rec(4) As Vector2
        Rec(0) = New Vector2(XStart, YStart)
        Rec(1) = New Vector2(XStart + GesamtBreite, YStart)
        Rec(2) = New Vector2(XStart + GesamtBreite, YStart + BuchstabenDimension.Height + 4)
        Rec(3) = New Vector2(XStart, YStart + BuchstabenDimension.Height + 4)
        Rec(4) = New Vector2(XStart, YStart)

        Grafikengine.DrawLine(Rec, Color.Black, 3, False)
        For I = 0 To Breiten.Length - 2
            Dim B1 As Integer = 0
            For I2 = 0 To I
                B1 += Breiten(I2)
            Next
            Grafikengine.DrawLine(New Vector2(B1 + XStart, YStart), New Vector2(B1 + XStart, YStart + BuchstabenDimension.Height + 4), Color.Black, 3, False)
        Next
        Dim MaxItems As Integer = Math.DivRem(Hoehe, BuchstabenDimension.Height + 4, 0)
        If MaxItems > Games.Count Then MaxItems = Games.Count
        For I = 0 To MaxItems - 1
            Rec(0).Y += BuchstabenDimension.Height + 4
            Rec(1).Y += BuchstabenDimension.Height + 4
            Rec(2).Y += BuchstabenDimension.Height + 4
            Rec(3).Y += BuchstabenDimension.Height + 4
            Rec(4).Y += BuchstabenDimension.Height + 4
            Grafikengine.DrawLine(Rec, Color.Black, 1, False)
        Next
        For I = 0 To Breiten.Length - 2
            Dim B1 As Integer = 0
            For I2 = 0 To I
                B1 += Breiten(I2)
            Next
            Grafikengine.DrawLine(New Vector2(B1 + XStart, YStart + BuchstabenDimension.Height + 4), New Vector2(B1 + XStart, YStart + (BuchstabenDimension.Height + 4) * (MaxItems + 1)), Color.Black, 1, False)
        Next

        If CurrentGame IsNot Nothing Then
            For I = FirstGame To FirstGame + MaxItems - 1
                If Games(I).GameID = CurrentGame.GameID Then
                    Rec(0) = New Vector2(XStart, YStart + (BuchstabenDimension.Height + 4) * (I - FirstGame + 1))
                    Rec(1) = New Vector2(XStart + GesamtBreite, YStart + (BuchstabenDimension.Height + 4) * (I - FirstGame + 1))
                    Rec(2) = New Vector2(XStart + GesamtBreite, YStart + BuchstabenDimension.Height + 4 + (BuchstabenDimension.Height + 4) * (I - FirstGame + 1))
                    Rec(3) = New Vector2(XStart, YStart + BuchstabenDimension.Height + 4 + (BuchstabenDimension.Height + 4) * (I - FirstGame + 1))
                    Rec(4) = New Vector2(XStart, YStart + (BuchstabenDimension.Height + 4) * (I - FirstGame + 1))
                    Grafikengine.DrawLine(Rec, Color.Red, 3, False)
                    Exit For
                End If
            Next
        End If

        Grafikengine.Sprite.Begin(SpriteFlags.AlphaBlend)

        'Scrollbar+Berechnung
        Scrollbar.Malen()
        If Games.Count > MaxItems Then
            FirstGame = (Games.Count - MaxItems) * Scrollbar.GetValuePos
        Else
            FirstGame = 0
        End If

        Dim B As Integer = 0
        'Host Name anzeigen
        Dim Spielname(0) As String
        Spielname(0) = ShortenWort(Sprachconverter.Translate("Host"), Breiten(0) - 4, BuchstabenDimension.Width)
        Text.neuerText(Spielname, XStart + Breiten(0) / 2 - Spielname(0).Length / 2 * BuchstabenDimension.Width, YStart + 2, Nothing, , 4)
        Text.Malen(New Point)
        B = Breiten(0)
        'Spielname anzeigen
        Spielname(0) = ShortenWort(Sprachconverter.Translate("Spielname"), Breiten(1) - 4, BuchstabenDimension.Width)
        Text.neuerText(Spielname, XStart + B + Breiten(1) / 2 - Spielname(0).Length / 2 * BuchstabenDimension.Width, YStart + 2, Nothing, , 4)
        Text.Malen(New Point)
        B += Breiten(1)
        'Spielerzahl anzeigen
        Spielname(0) = ShortenWort(Sprachconverter.Translate("Spieler"), Breiten(2) - 4, BuchstabenDimension.Width)
        Text.neuerText(Spielname, XStart + B + Breiten(2) / 2 - Spielname(0).Length / 2 * BuchstabenDimension.Width, YStart + 2, Nothing, , 4)
        Text.Malen(New Point)
        B += Breiten(2)
        'Versionsnummer
        Spielname(0) = ShortenWort(Sprachconverter.Translate("Version"), Breiten(3) - 4, BuchstabenDimension.Width)
        Text.neuerText(Spielname, XStart + B + Breiten(3) / 2 - Spielname(0).Length / 2 * BuchstabenDimension.Width, YStart + 2, Nothing, , 4)
        Text.Malen(New Point)
        B += Breiten(3)
        'Passwrot Ja Nein?
        Spielname(0) = ShortenWort(Sprachconverter.Translate("Passwort"), Breiten(4) - 4, BuchstabenDimension.Width)
        Text.neuerText(Spielname, XStart + B + Breiten(4) / 2 - Spielname(0).Length / 2 * BuchstabenDimension.Width, YStart + 2, Nothing, , 4)
        Text.Malen(New Point)
        'Username anzeigen
        If Games.Count - 1 < MaxItems - 1 Then
            ReDim Spielname(Games.Count - 1)
        Else
            ReDim Spielname(MaxItems - 1)
        End If

        For I = FirstGame To FirstGame + MaxItems - 1
            Dim Member As MemberInfo = FindMember(Games(I).CreatorID)
            If Member IsNot Nothing Then
                Spielname(I - FirstGame) = ShortenWort(Member.Username, Breiten(0) - 4, BuchstabenDimension.Width)
            Else
                Spielname(I - FirstGame) = ShortenWort("Member not found!", Breiten(0) - 4, BuchstabenDimension.Width)
            End If

        Next
        Text.neuerText(Spielname, XStart + 4, YStart + 2 + BuchstabenDimension.Height + 4, Nothing, , 4)
        Text.Malen(New Point)
        'Spielname anzeigen
        For I = FirstGame To FirstGame + MaxItems - 1
            Spielname(I - FirstGame) = ShortenWort(Games(I).Name, Breiten(1) - 4, BuchstabenDimension.Width)
        Next
        B = Breiten(0)
        Text.neuerText(Spielname, XStart + 4 + B, YStart + 2 + BuchstabenDimension.Height + 4, Nothing, , 4)
        Text.Malen(New Point)

        'Spieleranzahl anzeigen
        For I = FirstGame To FirstGame + MaxItems - 1
            Spielname(I - FirstGame) = ShortenWort((1 + Games(I).CurrentPlayers).ToString + "/" + Games(I).MaxPlayers.ToString, Breiten(2) - 4, BuchstabenDimension.Width)
        Next
        B += Breiten(1)
        Text.neuerText(Spielname, XStart + 4 + B, YStart + 2 + BuchstabenDimension.Height + 4, Nothing, , 4)
        Text.Malen(New Point)
        'Versionen anzeigen
        For I = FirstGame To FirstGame + MaxItems - 1
            Spielname(I - FirstGame) = ShortenWort(Games(I).Version.ToString, Breiten(3) - 4, BuchstabenDimension.Width)
        Next
        B += Breiten(2)
        Text.neuerText(Spielname, XStart + 4 + B, YStart + 2 + BuchstabenDimension.Height + 4, Nothing, , 4)
        Text.Malen(New Point)
        'Passwort anzeigen
        For I = FirstGame To FirstGame + MaxItems - 1
            Spielname(I - FirstGame) = ShortenWort(Sprachconverter.Translate(Games(I).Flags.ToString), Breiten(4) - 4, BuchstabenDimension.Width)
        Next
        B += Breiten(3)
        Text.neuerText(Spielname, XStart + 4 + B, YStart + 2 + BuchstabenDimension.Height + 4, Nothing, , 4)
        Text.Malen(New Point)
        Updating = False
    End Sub
    Private Function ShortenWort(ByVal Wort As String, ByVal Breite As Integer, ByVal Buchstabenbreite As Integer) As String
        Dim Buchstabenzahl As Integer = Math.DivRem(Breite, Buchstabenbreite, 0)
        Dim Rueckgabe As String = ""
        If Wort.Length > Buchstabenzahl Then
            Rueckgabe = Wort.Substring(0, Buchstabenzahl - 3) + "..."
        Else
            Rueckgabe = Wort
        End If
        Return Rueckgabe
    End Function

    Public Function MouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        Select Case e.Button
            Case MouseButtons.Right

            Case MouseButtons.Left
                Scrollbar.MouseDown(Nothing, e)
                Dim BuchstabenDimension As Size = Text.GetSchriftart.MeasureString(Grafikengine.Sprite, "A", DrawTextFormat.None, Color.Black).Size
                Dim GesamtBreite As Integer = 0
                For I = 0 To Breiten.Length - 1
                    GesamtBreite += Breiten(I)
                Next
                If e.X > XStart And e.X < XStart + GesamtBreite Then
                    Dim Item As Integer = Math.DivRem(e.Y - YStart, (BuchstabenDimension.Height + 4), 0) + FirstGame
                    If Games.Count > Item - 1 And Item > 0 Then
                        CurrentGame = Games(Item - 1)
                    End If
                End If
        End Select
        Return True
    End Function
    Public Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Scrollbar.MouseEnter(Nothing, e)
    End Sub
    Public Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Scrollbar.MouseUp(Nothing, e)
    End Sub
    ''' <summary>
    ''' Finds the new ID for a newly joined Player
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function FindNewMemberID() As Integer
        Dim NewID As Integer = Integer.MinValue
        While MemberIDExists(NewID)
            NewID += 1
        End While
        Return NewID
    End Function
    Private Function MemberIDExists(ByVal ID As Integer) As Boolean
        For Each Member In Members
            If Member.ID = ID Then
                Return True
            End If
        Next
        Return False
    End Function
    ''' <summary>
    ''' Is called from a Game when we won't to launch it.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RemoveGameFromLobby()
        'Only Host can send this Info
        If CurrentGame IsNot Nothing Then
            If Hauptmenue.Connection.Hosten Then
                Connection.SendData("RemoveGame:" + CurrentGame.GameID.ToString)
                If DataSettings.LobbyHost Then
                    RemoveGame(CurrentGame.GameID)
                End If
            End If
        End If
        If Not DataSettings.LobbyHost Then 'Lobby Host should never leave the Lobby could produce some Interesting results though.
            'We need to kill ourself! (Remove us from the Lobby only hopefully!
            Connection.SendData("KillPlayer:" + ClientID.ToString)
        End If
    End Sub
    ''' <summary>
    ''' Is called from a Host  when returning to the selection or Rule Screen
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CancelGameFromLobby()
        'Only Host can send this Info
        If CurrentGame IsNot Nothing Then
            If Hauptmenue.Connection.Hosten Then

                Dim RemoveID As UInteger = CurrentGame.GameID
                If DataSettings.LobbyHost Then
                    RemoveGame(CurrentGame.GameID)
                End If
                Connection.SendData("RemoveGame:" + RemoveID.ToString)
            End If
        End If
    End Sub
    ''' <summary>
    ''' Removes a specifc Game from the list.
    ''' </summary>
    ''' <param name="GameID"></param>
    ''' <remarks></remarks>
    Private Sub RemoveGame(ByVal GameID As UInteger)
        If DataSettings.LobbyHost Then
            For I = 0 To Games.Count - 1
                If Games(I).GameID = GameID Then
                    Games.RemoveAt(I)
                    If CurrentGame IsNot Nothing Then
                        If CurrentGame.GameID = GameID Then
                            CurrentGame = Nothing
                        End If
                    End If
                    Exit For
                End If
            Next
        End If
    End Sub
    ''' <summary>
    ''' Callled when somebody in the Lobby creates a new Game
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub NewGameStarted(Optional ByVal Password As String = "")
        If Hauptmenue.Connection.Hosten Then
            Dim M As ObjektNetzwerkMenue = Objektverwalter.Menue
            If DataSettings.LobbyHost Then
                'A lot easier than the second case.
                Games.Add(New GameInfo(FindGameID, ClientID, M.FondsproStadt.Length - 1, M.GetKartenname, My.Application.Info.Version, Password, GetIP()))
                CurrentGame = Games(Games.Count - 1)
            Else
                Connection.SendData("GameCreated:" + ClientID.ToString + ":" + (M.FondsproStadt.Length - 1).ToString + ":" + M.GetKartenname + ":" + My.Application.Info.Version.ToString + ":" + Password + ":" + GetIP().ToString)
                CurrentGame = New GameInfo(FindGameID, ClientID, M.FondsproStadt.Length - 1, M.GetKartenname, My.Application.Info.Version, Password, GetIP())
            End If
        End If
    End Sub
    Public Function FindMember(ByVal ID As Integer) As MemberInfo
        For Each Member In Members
            If Member.ID = ID Then
                Return Member
            End If
        Next
        Return Nothing
    End Function
    ''' <summary>
    ''' Called by the Host Connection when a Playyer enters the Lobby.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub PlayerCreated(ByRef ID As Integer)
        ID = FindNewMemberID()
        Members.Add(New MemberInfo(ID, MemberInfo.MemberFlag.Lobby)) 'Create a new Member.
    End Sub
    ''' <summary>
    ''' Kill this Player immediatly.
    ''' </summary>
    ''' <param name="PlayerID"></param>
    ''' <remarks></remarks>
    Public Sub KillPlayer(ByVal PlayerID As Integer)
        For I = 0 To Members.Count - 1
            If Members(I).ID = PlayerID Then
                Members.RemoveAt(I) 'Now this Player is dead.
                Exit For
            End If
        Next
    End Sub
    Private Function FindGameID() As UInteger
        Dim ID As UInteger = 0
        While True
            ID += 1
            For Each Game In Games
                If ID = Game.GameID Then
                    Continue While
                End If
            Next
            Return ID
        End While
    End Function
    Public Function CompareVersion(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
        If Version1.Build = Version2.Build Then
            If Version1.Major = Version2.Major Then
                If Version1.MajorRevision = Version2.MajorRevision Then
                    If Version1.Minor = Version2.Minor Then
                        If Version1.MinorRevision = Version2.MinorRevision Then
                            If Version1.Revision = Version2.Revision Then
                                Return True
                            End If
                        End If
                    End If
                End If
            End If
        End If
        Return False
    End Function
    Public Sub SendLobbyUpdate()
        If DataSettings.LobbyHost Then
            If UpdateLobby > LobbyUpdateTime Then
                Connection.SendData("UpdateGameStart")
                Dim I As Integer = 0
                While I < Games.Count
                    If Games(I).WrongGame Then
                        Games.RemoveAt(I)
                    Else
                        Connection.SendData(Games(I).tostring)
                        I += 1
                    End If
                End While
                Connection.SendData("UpdateGameEnd")
                UpdateLobby = 0
            Else
                UpdateLobby += 1
            End If
        End If
    End Sub


End Class



